package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Address;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("address")
@RestController
@Api(tags = "AddressController",description = "前台-地址相关")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 查询地址列表
     * @param httpServletRequest
     * @param page
     * @return
     */
    @RequestMapping(value = "getAddressList.do",method = RequestMethod.POST)
    @ApiOperation("地址管理-列表")
    public ServerResponse<PageInfo<List<Address>>> getAddressList(HttpServletRequest httpServletRequest, Page page){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return addressService.getAddressList(user.getId(),page);
    }

    /**
     * 新增收货地址
     * @param address
     * @return
     */
    @RequestMapping(value = "addAddress.do",method = RequestMethod.POST)
    @ApiOperation("地址管理-新增")
    public ServerResponse addAddress(HttpServletRequest httpServletRequest,Address address){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        address.setUserId(user.getId());
        return addressService.addAddress(address);
    }

    /**
     * 地址管理-设为默认
     * @param id
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "updateAddress.do",method = RequestMethod.POST)
    @ApiOperation("地址管理-设为默认")
    public ServerResponse updateAddress(@RequestParam Long id,HttpServletRequest httpServletRequest){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return addressService.updateAddress(id,user.getId());
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @RequestMapping(value = "delAddress.do",method = RequestMethod.POST)
    @ApiOperation("地址管理-删除地址")
    public  ServerResponse delAddress(@RequestParam Long id){

        return addressService.delAddress(id);
    }


}
